/******************************************************************************
**                                                                           **
**                 Proprietary and Confidential Information                  **
**                                                                           **
**                 Copyright (c) Conexant Systems, Inc. Unpublished                  **
**                                                                           **
** This source code and the algorithms implemented therein constitute        **
** confidential information and may compromise trade secrets of Conexant Systems,    **
** Inc. or its associates, and any unauthorized use thereof is prohibited.   **
**                                                                           **
**                                                                           **
**       Copyright (C) 1991, 1992, Thomas G. Lane.                           **
** This file is a part of the Independent JPEG Group's software.             **
** ************************************************************************* */

#ifndef __JPEGTERM_H__
#define __JPEGTERM_H__

/******************************************************************************
*  FILE FORMAT:
*      1. ENUMERATIONS 
*      2. DEFINES
*      3. STRUCTURES
*      4. ALLOCS
*      5. FUNCTIONS
******************************************************************************/

/******************************************************************************
*  1. ENUMERATIONS 
******************************************************************************/

/******************************************************************************
*  2. DEFINES
******************************************************************************/

#define M_SOF0  0xC0
#define M_SOF7  0xC7  
#define M_JPG	  0xC8
#define M_SOF9	0xC9
#define M_SOF10 0xCA
#define M_SOF15 0xCF  
#define M_DHT   0xC4 
#define M_DAC   0xCC  
#define M_RST0  0xD0
#define M_RST7  0xD7  
#define M_SOI   0xD8
#define M_EOI   0xD9
#define M_SOS   0xDA
#define M_DQT   0xDB
#define M_DNL   0xDC
#define M_DRI   0xDD
#define M_DHP   0xDE
#define M_EXP   0xDF
#define M_APP0  0xE0
#define M_APP1  0xE1
#define M_APPF	0xEF
#define M_JPG0	0xF0
#define M_JPG13 0xFD
#define M_COM   0xFE
#define M_TEM   0x01  
#define M_ERROR 0x100

#define MAXSAMPLE 255

/// The basic DCT block is 8x8 samples 
#define DCTSIZE		8
/// DCTSIZE squared; # of elements in a block
#define DCTSIZE2	64

#define MARKER_SOI_BIT    0x0001
#define MARKER_SOF_BIT    0x0002
#define MARKER_SOS_BIT    0x0004
#define MARKER_DQT_BIT    0x0008
#define MARKER_DHT_BIT    0x0010
#define MARKER_G3FAX_BIT  0x4000
#define MARKER_DNL_BIT    0x8000

/// quantization tables are numbered 0..3
#define NUM_QUANT_TBLS      4   // 4

/// Huffman tables are numbered 0..3
#define NUM_HUFF_TBLS       2   // 4

/// JPEG limit on # of components in one scan
#define MAX_COMPS_IN_SCAN   3   //4

/// JPEG limit on # of blocks in an MCU
#define MAX_BLOCKS_IN_MCU   6   //10

/******************************************************************************
*  3. STRUCTURES
******************************************************************************/

typedef struct {
  /// component's image width in samples
  //UINT32 comp_width;

  /// component's image height in samples
  //UINT32 comp_height;

  /// identifier for this component (0..255)
  UINT8 component_id;

  /// its index in SOF or cinfo->comp_info[]
  UINT8 component_index;

  /// horizontal sampling factor (1..4)
  UINT8 h_samp_factor;

  /// vertical sampling factor (1..4)
  UINT8 v_samp_factor;

  /// quantization table selector (0..3)
  UINT8 quant_tbl_no;

  /// DC entropy table selector (0..3)
  UINT8 dc_tbl_no;

  /// AC entropy table selector (0..3)
  UINT8 ac_tbl_no;

  /// number of blocks per MCU, horizontally
  //UINT32 mcu_width;

  /// number of blocks per MCU, vertically
  //UINT32 mcu_height;

  /// MCU_width * MCU_height
  UINT32 mcu_blocks;

} JPEG_COMPONENT_INFO;

typedef struct {    
  UINT8 bits[17];
  UINT8 huffval[256];
  
} JPEG_HUFF_TBL;

typedef struct {
  /// input image width
  UINT16 image_width;

  /// input image height
  UINT16 image_height;
  
  /// # of MCUs across the image
  //UINT16 n_mcus_per_line;

  /// # of MCU rows in the image
  //UINT16 n_mcu_lines;
  
  /// MCUs per restart interval, or 0 for no restart
  UINT16 restart_interval;

  /// MCUs left in this restart interval
  UINT16 restart_to_go;

  /// last DC coef for each comp
  INT16 last_dc_val[MAX_COMPS_IN_SCAN];

  /// bits of precision in image data
  UINT8 data_precision;

  /// # of color components in JPEG image
  UINT8 num_components;

  /// largest h_samp_factor
  UINT8 max_h_samp_factor;

  /// largest v_samp_factor
  UINT8 max_v_samp_factor;

  /// # of JPEG components output this time
  UINT8 comps_in_scan;

  /// # of DCT blocks per MCU
  UINT8 blocks_in_mcu;

  UINT8 mcu_membership[MAX_BLOCKS_IN_MCU];

  /// # of next RSTn marker (0..7)
  UINT8 next_restart_num;

} JPEG_COMPRESS_INFO;

/******************************************************************************
*  4. ALLOCS
******************************************************************************/

#ifdef ALLOC_JPEG_TREM_TABLES

UINT8 jpeg_zigzag[DCTSIZE2+16] = {
  0,  1,  8, 16,  9,  2,  3, 10,
 17, 24, 32, 25, 18, 11,  4,  5,
 12, 19, 26, 33, 40, 48, 41, 34,
 27, 20, 13,  6,  7, 14, 21, 28,
 35, 42, 49, 56, 57, 50, 43, 36,
 29, 22, 15, 23, 30, 37, 44, 51,
 58, 59, 52, 45, 38, 31, 39, 46,
 53, 60, 61, 54, 47, 55, 62, 63,
  0,  0,  0,  0,  0,  0,  0,  0, /* extra entries in case k>63 below */
  0,  0,  0,  0,  0,  0,  0,  0
};

UINT8 jpeg_dc_luminance_bits[17] =
    { 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
UINT8 jpeg_dc_luminance_val[] =
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

UINT8 jpeg_dc_chrominance_bits[17] =
    { 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
UINT8 jpeg_dc_chrominance_val[] =
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
UINT8 jpeg_ac_luminance_bits[17] =
    { 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
UINT8 jpeg_ac_luminance_val[] =
    { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
      0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
      0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
      0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
      0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
      0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
      0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
      0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
      0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
      0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
      0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
      0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
      0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
      0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
      0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
      0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
      0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
      0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
      0xf9, 0xfa };

UINT8 jpeg_ac_chrominance_bits[17] =
    { 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
UINT8 jpeg_ac_chrominance_val[] =
    { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
      0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
      0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
      0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
      0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
      0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
      0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
      0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
      0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
      0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
      0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
      0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
      0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
      0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
      0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
      0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
      0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
      0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
      0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
      0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
      0xf9, 0xfa };

UINT8 jpeg_std_luminance_quant_tbl[DCTSIZE2] = {
    16,  11,  10,  16,  24,  40,  51,  61,
    12,  12,  14,  19,  26,  58,  60,  55,
    14,  13,  16,  24,  40,  57,  69,  56,
    14,  17,  22,  29,  51,  87,  80,  62,
    18,  22,  37,  56,  68, 109, 103,  77,
    24,  35,  55,  64,  81, 104, 113,  92,
    49,  64,  78,  87, 103, 121, 120, 101,
    72,  92,  95,  98, 112, 100, 103,  99
    };

UINT8 jpeg_std_chrominance_quant_tbl[DCTSIZE2] = {
   17,  18,  24,  47,  99,  99,  99,  99,
    18,  21,  26,  66,  99,  99,  99,  99,
    24,  26,  56,  99,  99,  99,  99,  99,
    47,  66,  99,  99,  99,  99,  99,  99,
    99,  99,  99,  99,  99,  99,  99,  99,
    99,  99,  99,  99,  99,  99,  99,  99,
    99,  99,  99,  99,  99,  99,  99,  99,
    99,  99,  99,  99,  99,  99,  99,  99
    };
#else
extern UINT8 jpeg_zigzag[DCTSIZE2+16];
extern UINT8 jpeg_dc_luminance_bits[17];
extern UINT8 jpeg_dc_luminance_val[];
extern UINT8 jpeg_dc_chrominance_bits[17];
extern UINT8 jpeg_dc_chrominance_val[];
extern UINT8 jpeg_ac_luminance_bits[17];
extern UINT8 jpeg_ac_luminance_val[];
extern UINT8 jpeg_ac_chrominance_bits[17];
extern UINT8 jpeg_ac_chrominance_val[];
extern UINT8 jpeg_std_luminance_quant_tbl[DCTSIZE2];
extern UINT8 jpeg_std_chrominance_quant_tbl[DCTSIZE2];
#endif

/******************************************************************************
*  5. FUNCTIONS
******************************************************************************/

#endif
